const app = getApp();
const util = require('../../utils/util.js')

Page({
    data: {
        inputShowed: false,
        inputVal: "",
        name: '',
        username:'',
        password:'',
        prompt: '',
        noResult: false,
        users: [],
        index:'',
        isAdd:false,

    },
    onLoad: function () {
        wx.setNavigationBarTitle({
            title: '选择用户'
        })
        // this.getAreaList(this.data.longitude,this.data.latitude,this.data.inputVal)

    },
    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    hideInput: function () {
        this.setData({
            inputVal: "",
            inputShowed: false,
            isAdd: false,
        });
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
        });
    },
    inputTyping: function (e) {

        this.setData({
            inputVal: e.detail.value,
        })
        this.getUserList(this.data.inputVal);
        // this.getAreaList(this.data.longitude,this.data.latitude,this.data.inputVal)

    },
    getUserList: function (inputValue) {
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/admin/user/getUserFast',
            data: {
                token: app.globalData.token,
                searchStr: inputValue,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {

                    if (res.data.records.list.length == 0) {
                        that.setData({
                            prompt: '没有找到相关用户,请确认输入正确',
                            noResult: true,
                            users: []
                        })
                    } else {
                        console.log(that.data.users)
                        that.setData({
                            users: res.data.records.list,
                            noResult: false
                        })
                        console.log(that.data.users)
                    }
                }
            },
            fail: function (err) {
                console.log(err)
            }
        })
    },
    selected: function (e) {

        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        var data = e.currentTarget.dataset;
        var param = {
            username:data.name,
            id:data.id
        };
        prevPage.setData({
            selectUser:param
        });
        wx.navigateBack({
            delta: 1
        })
    },
    name: function (e) {
        this.setData({
            name:e.detail.value
        })
    },
    username: function (e) {
        this.setData({
            username:e.detail.value
        })
    },
    password: function (e) {
        this.setData({
            password:e.detail.value
        })
    },
    bindAddUser: function () {
        this.setData({
            isAdd: true
        })
    },
    cancelAdd: function () {
        this.setData({
            isAdd: false
        })
    },
    saveAdd: function () {
        console.log(this.data.username)
        wx.showLoading({
            title: '保存中'
        })
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/admin/user/save',
            data: {
                token: app.globalData.token,
                name: that.data.name,
                username: that.data.username,
                password: that.data.password,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    wx.showToast({
                        title: '添加成功',
                        icon: 'success',
                        duration: 3000
                    });
                    var data = res.data.record;
                    let pages = getCurrentPages();
                    let prevPage = pages[pages.length - 2];
                    var param = {
                        username:data.name,
                        id:data.id
                    };
                    prevPage.setData({
                        selectUser:param
                    });
                    wx.navigateBack({
                        delta: 1
                    })
                }
            },
            fail: function (err) {
                console.log(err)
            }
        })
    }
});